import { fetchTypes } from '@/api/types'
import { processTypes } from '@/util/types'

export default {
  namespaced: true,
  state: {
    types: []
  },
  getters: {
    types: state => state.types,
    options: state => {
      return processTypes(state.types)
    }
  },
  mutations: {
    setTypes: (state, val) => {
      state.types = val
    }
  },
  actions: {
    fetchTypes: ({ commit }) => {
      return new Promise((reslove, reject) => {
        fetchTypes()
          .then(types => {
            commit('setTypes', types)
            reslove(types)
          })
          .catch(reject)
      })
    }
  }
}
